package com.dcy.znoa.controller.publiz;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.carPark.AddCarParkModel;
import com.dcy.znoa.controller.publiz.model.carPark.LogicDelCarParkModel;
import com.dcy.znoa.controller.publiz.model.carPark.SelectCarParkModel;
import com.dcy.znoa.controller.publiz.model.carPark.UpdateCarParkModel;
import com.dcy.znoa.domain.publiz.Canteen;
import com.dcy.znoa.domain.publiz.CarPark;
import com.dcy.znoa.service.publiz.CarParkService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

import static com.dcy.znoa.common.util.pojo.JsonCommon.*;

/**
 * @author pengyu
 */
//停车场 controller
@Controller
@RequestMapping("carPark")
public class CarParkController extends BaseController {
    @Autowired
    CarParkService carParkService;


    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(@Valid SelectCarParkModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonForList("404",bindingResult.getFieldError().getDefaultMessage());
        }
        return carParkService.selectList(model);
//        List list = canteenService.selectList(model);
//        if(list.isEmpty()){
//            return new JsonForList(NOT_FOUND_CODE);
//        }else{
//            return new JsonForList(list);
//        }
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddCarParkModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        if(model.getEntityType()==null){
            model.setEntityType(1);//	所属类型 1 园区 2 企业
        }
        model.setCreateOperId(getCurrentUser().getOperId());
        CarPark cp = new CarPark();
        BeanUtils.copyProperties(cp, model);
//        cp.setResidualCapacity((int)(Math.random() * (cp.getCapacity()==null?0:cp.getCapacity())));
        Integer result =  carParkService.insert(cp);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(INSERT_ERROR_CODE);
        }
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    public JsonCommon updateById(@Valid UpdateCarParkModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        CarPark cp = new CarPark();
        BeanUtils.copyProperties(cp, model);
        Integer result =  carParkService.updateById(cp);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(UPDATE_ERROR_CODE);
        }
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/valid")
    public JsonCommon valid(@Valid LogicDelCarParkModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        CarPark cp = new CarPark();
        BeanUtils.copyProperties(cp, model);
        Integer result = carParkService.updateById(cp);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(DELETE_ERROR_CODE);
        }
    }

}
